var Index = require('../app/controllers/index')
var User = require('../app/controllers/user')
var Movie = require('../app/controllers/movie')
var Comment = require('../app/controllers/comment')
var Category = require('../app/controllers/category')
var multipart = require('connect-multiparty')
var multipartMiddleware = multipart()

module.exports = function (app) {
    app.use(multipart())

// 预处理 user pre handle user
    app.use(function (req, res, next) {
        // console.log('user in session:')
        // console.log(req.session.user)
        var _user = req.session.user
        // 没有就是空 ，这样 User.logout 中就不用 delete app.locals.user了
        app.locals.user = _user  // 把 _user 放到本地变量 user 中
        return next()
    })
    // index
    app.get('/', Index.index)
    // user
    app.post('/user/signup', User.signup)
    app.post('/user/signin', User.signin)
    app.get('/signin', User.showSignin)
    app.get('/signup', User.showSignup)
    app.get('/logout', User.logout)
    app.get('/admin/user/list', User.signinRequired, User.adminRequired, User.list)
    // movie
    app.get('/movie/:id', Movie.detail, User.signinRequired)
    app.get('/admin/movie/new',  User.signinRequired, User.adminRequired, Movie.new)
    app.get('/admin/movie/update/:id',  User.signinRequired, User.adminRequired, Movie.update)
    // savePoster 之后才 save 因为文件读取是异步的，所以必须文件上传成功之后，再更新电影相关字段
    app.post('/admin/movie/new',  multipartMiddleware, User.signinRequired, User.adminRequired, Movie.savePoster, Movie.save)
    app.get('/admin/movie/list',  User.signinRequired, User.adminRequired, Movie.list)
    app.delete('/admin/movie/list',  User.signinRequired, User.adminRequired, Movie.del)
    // comment
    app.post('/user/comment',  User.signinRequired, Comment.save)
    // category
    app.get('/admin/category/new', User.signinRequired, User.adminRequired, Category.new)
    app.post('/admin/category', User.signinRequired, User.adminRequired, Category.save)
    app.get('/admin/category/list', User.signinRequired, User.adminRequired, Category.list)
    // results
    app.get('/results', Index.search)


}